﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace AEM.Controls
{
    /// <summary>
    /// Statements.xaml 的交互逻辑
    /// </summary>
    public partial class Statements : UserControl
    {
        private Point _startPoint;
        public Statements()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(TitleBar_MouseLeftButtonDown);
            this.TitleBar.MouseMove += new MouseEventHandler(TitleBar_MouseMove);
            this.sys_button_close.Click += new RoutedEventHandler(sys_button_close_Click);
        }

        #region 拖动功能
        private void StartDrag(MouseEventArgs e)
        {
            Point thisPoint = e.GetPosition(null);
            double left = 0, top = 0;
            left = this.Margin.Left;
            top = this.Margin.Top;
            left = thisPoint.X - _startPoint.X + left;
            top = thisPoint.Y - _startPoint.Y + top;
            double maxtop = System.Windows.SystemParameters.WorkArea.Height - this.Height;
            double maxleft = System.Windows.SystemParameters.WorkArea.Width - this.Width;
            if (left > maxleft) left = maxleft;
            if (top > maxtop) top = maxtop;
            if (left < 10) left = 10;
            if (top < 10) top = 10;
            this.Margin = new Thickness(left, top, 0, 0);

            _startPoint = thisPoint;
        }

        void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point position = e.GetPosition(null);
                if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    this.StartDrag(e);
                }
            }
        }

        void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _startPoint = e.GetPosition(null);
        }
        #endregion

        void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            Close.Invoke(this, new EventArgs());
        }

       public EventHandler Close;
    }
}
